var {isPhoneAvailable, formatDate} = require('../../../utils/util')
var app = getApp()
var delayflag

Page({
  data: {
    needMsg: false,
    msgPhone: '',
    msgCode: '',
    verifyTips: '获取验证码',
    timeflag: -1
  },

  onLoad: function (options) {
    var info
    if (app.globalData.personInfo) {
      info = app.globalData.personInfo
      info.SEXVAL = app.globalData.sexValues[parseInt(info.USERSEX)]
    }
    else {
      info = {
        USERNAME: '',
        USERSEX: '2',
        SEXVAL: '保密',
        MOBILE: '',
        USERBIRTHDAY: ' 1990-01-01'
      }
    }
    var curdate = formatDate(new Date(), 'yyyy-MM-dd')
    this.setData({
      info: info,
      endtime: curdate,
      sexArr: app.globalData.sexValues
    })
  },

  onHide: function () {
    if (delayflag) {
      clearTimeout(delayflag)
    }
  },
  onUnload: function () {
    if (delayflag) {
      clearTimeout(delayflag)
    }
  },

  bindSexChange: function (e) {
    var info = this.data.info
    if (e.detail.value == 0) {
      info.USERSEX = 0
      info.SEXVAL = '女'
    }
    else if (e.detail.value == 1) {
      info.USERSEX = 1
      info.SEXVAL = '男'
    }
    else if (e.detail.value == 2) {
      info.USERSEX = 2
      info.SEXVAL = '保密'
    }
    this.setData({
      info: info
    })
  },
  bindDateChange: function (e) {
    var info = this.data.info
    info.USERBIRTHDAY = e.detail.value
    this.setData({
      info: info
    })
  },
  checkInput: function (e) {
    var itype = e.currentTarget.dataset.inputtype
    var info = this.data.info
    if (itype === 'name') {
      info.USERNAME = e.detail.value
    }
    else if (itype === 'phone') {
      info.MOBILE = e.detail.value
    }
    this.setData({
      info: info
    })
  },
  phoneTap: function () {
    this.setData({
      needMsg: true
    })
  },
  submitInfo: function () {
    if (this.data.info.USERNAME === '' || this.data.info.MOBILE === '') {
      app.toast.error('信息不完整')
      return
    }
    if (!isPhoneAvailable(this.data.info.MOBILE)) {
      app.toast.error('手机号码格式不正确')
      return
    }
    if (this.data.needMsg) {
      if (this.data.msgCode === '') {
        return
      }
    }
    var url = '/yuanmai_we/usercenter/user/registerForVip'
    var params = {
      __zaofans: true,
      USERNAME: this.data.info.USERNAME,
      USERSEX: this.data.info.USERSEX,
      MOBILE: this.data.info.MOBILE,
      USERBIRTHDAY: this.data.info.USERBIRTHDAY
    }
    if (this.data.needMsg) {
      params.CHECKCODE = this.data.msgCode
    }
    app.request(url, params).then(this.submitCallBack.bind(this))
  },
  submitCallBack: function (res) {
    if (res.success) {
      app.toast.success('修改成功')
      app.globalData.personInfo = {
        USERNAME: this.data.info.USERNAME,
        USERSEX: this.data.info.USERSEX,
        MOBILE: this.data.info.MOBILE,
        USERBIRTHDAY: this.data.info.USERBIRTHDAY
      }
      setTimeout(() => {
        wx.navigateBack()
      }, 1000)
    }
    else {
      app.toast.error(res.error)
    }
  },
  msgPhoneInput: function (e) {
    this.setData({
      msgPhone: e.detail.value
    })
  },
  msgCodeInput: function (e) {
    this.setData({
      msgCode: e.detail.value
    })
  },
  sendMsgAction: function (e) {
    if (this.data.msgPhone === '') {
      app.toast.error('请输入手机号码')
      return
    }
    if (!isPhoneAvailable(this.data.msgPhone)) {
      app.toast.error('手机号码格式不正确')
      return
    }

    if (this.data.timeflag > 0) {
      return
    }
    this.startReductTime()

    var url = '/yuanmai_we/activity/sms/sendCheckCode'
    var property = {
      "MOBILE": this.data.msgPhone,
      "ACTION": "updateMobile"
    }
    var params = {
      datas: JSON.stringify(property),
      __zaofans: true
    }
    app.request(url, params).then(res => {
      if (res.success) {
        app.toast.success('发送成功')
      } else {
        app.toast.error(res.errMsg)
      }
    })
  },
  startReductTime: function () {
    var flag
    if (this.data.timeflag === -1) {
      flag = 60
    }
    else if (this.data.timeflag > 0) {
      flag = this.data.timeflag - 1
    }
    var tips = flag + '秒后重试'
    if (flag === 0) {
      flag = -1
      tips = '获取验证码'
    }
    this.setData({
      timeflag: flag,
      verifyTips: tips
    })
    if (flag < 0) {
      return
    }
    var that = this
    delayflag = setTimeout(() => {
      if (delayflag) {
        clearTimeout(delayflag)
        that.startReductTime()
      }
    }, 1000)
  }
})
